import puppeteer from 'puppeteer';
// const { convert } = require('html-to-text');
// import { ri } from './ri.js'
let browser;
let num = 0
async function runT(data, nextUrl) {
  const page = await browser.newPage();
  
  await page.goto(nextUrl, {
    timeout: 5 * 60 * 1000,
    waitUntil: "networkidle2"
  })
  let nextUrls = null;
  let htmlStr = null;
  console.log(await browser.version());
  const tNav = '.container .mybox .page1 a:last-child';
  const urlsss = await page.$eval('.container', a => {
    console.log('a----', a);
  });
  console.log('urlsss----', urlsss);
  
  
  // const text = await page.evaluate(tNav => {
  //   return document.querySelector(tNav).innerHTML;
  // }, tNav);
  // console.log('text----', text);
  // const nextBtn = await page.locator('.container .mybox .page1 a:last-child').filter(button => {
  //   console.log('button.innerText----', button.innerText);
  //   if (button.innerText === '下一章') {
  //     nextUrls = button.getAttribute('href')
  //   }
  //   return false
  // });
  // const nextBtnContent = await page.locator('.container .mybox > .txtnav').filter(button => {
  //  const body = button;
  //   body.removeChild(body.querySelector('.hide720')); 
  //   body.removeChild(body.querySelector('.hide720')); 
  //   body.removeChild(body.querySelector('#txtright')); 
  //   htmlStr = body.innerHTML;
  //   return false
  // });
  // console.log('nextUrl+++++', nextUrls);
  // data.string = data.string + htmlStr;
  // page.close();
  // let str='';
  // const bodyHandle = await page.locator('.container .mybox > .txtnav').filter(dom => {
  //   // console.log(d);
  //   str = dom.innerText;
  //   return true
  // });
  // console.log('-----str-', str);
  // console.log('-----html-', bodyHandle);
  // const html = await page.evaluate(body => body.innerHTML, bodyHandle);
  // console.log('-----html-', html);
  // await bodyHandle.dispose();
  page.close();
  return {
    html: htmlStr,
    url: nextUrls
  };
}
// }
const datas = {
  string: ''
};
async function scrape() {
  browser = await puppeteer.launch({
    browser: "firefox",
    headless: 'new',
  });
  let url = 'https://69shu.me/txt/54983/35279393';
  let num  = 0;
//  await runT(datas, url);
  while (url) {
    const res = await runT(datas, url);
    // console.log('前一秒');
    await new Promise(resolve => setTimeout(resolve, 100));
    console.log(num, url);
    url = res.url;
    num++;
  }
}
scrape().then(res => {
  // const options = {
  //   wordwrap: 130,
  // };
  // writeFile("./netflixscrape.txt", convert(datas.string, options), (err) => {
  //   if (err) {
  //     console.error(err);
  //     return;
  //   };
  //   console.log("Great Success");
  // });
  browser.close()
})
